
import 'package:blue_plugin/bean/dev_find.dart';
import 'package:blue_plugin/bean/dev_info.dart';
import 'package:blue_plugin/bean/dev_state.dart';
import 'package:blue_plugin/utils/toast_util.dart';
import 'package:blue_plugin_example/base/base_blue_controller.dart';
import 'package:blue_plugin_example/bean/dev_info_init.dart';
import 'package:blue_plugin_example/res/colors.dart';

class PlugRouteXTController extends BaseBlueController {
  String xt = "-";
  String xtEdit = "";
  var btnColor = ColorStyle.color_btn_bg_2;
  String connectMsg = "连接";
  String radioGroupValue = "空腹血糖";

  @override
  void onInit() {
    super.onInit();
    baseGetTime();
  }

  ///1.扫描设备
  onConnectBtn() {
    clearEditInfo();
    String uuid = DevInfoInit.DEV_XUETANG_UUID;
    String devName = DevInfoInit.DEV_XUETANG_NAME;
    List<String> _charaList = [];
    _charaList.insert(0, DevInfoInit.DEV_XUETANG_TZ_1);
    devInfo = DevInfo(DevInfoInit.DEV_TYPE_XUETANG, uuid, _charaList, devName,
        DEVFind.UUID, DevInfoInit.DEV_XUETANG_MARK);
    baseGetTime();
    baseOnConnectBtn(devInfo!);
  }

  ///2.设置按钮的状态值
  @override
  baseSetConnectMsg(DevState state, String error, String errorMsg) {
    print("baseSetConnectMsg$state--$error--$errorMsg");
    connectState = state;
    if (errorMsg.isNotEmpty) {
      ToastUtils.show(errorMsg);
    }
    if (state == DevState.CONNECT) {
      connectMsg = "连接";
    }

    if (state == DevState.CONNECTING) {
      connectMsg = "连接中";
    }

    if (state == DevState.RECONNECT) {
      connectMsg = "重新连接";
    }

    if (state == DevState.COLLECTIONING) {
      connectMsg = "采集中";
    }

    if (state == DevState.RECAPTURE) {
      connectMsg = "重新采集";
    }
    update();
  }

  ///3.根据设备类型 子类做处理
  @override
  devValueOpt(isSuccess, data, strdata, uuid) {
    if (isSuccess) {
      //成功
      print("收到的测量数据:${data}");
      if (devInfo!.devType == DevInfoInit.DEV_TYPE_XUETANG) {
        if (uuid == DevInfoInit.DEV_XUETANG_TZ_1) {
          baseSetConnectMsg(DevState.RECAPTURE, "4", "");
          double num = data[10] / 10;
          setXT(num);
          setEdit(num);
        }
      }
    }
  }

  onRadioClick(value) {
    radioGroupValue = value;
    clearEditInfo();
  }

  onUpDataBtn() {
    ToastUtils.showError("提交");
  }

  setBtnbg(bool flag) {
    if (flag) {
      btnColor = ColorStyle.color_btn_bg;
    } else {
      btnColor = ColorStyle.color_btn_bg_2;
    }
    update();
  }

  setXT(double value) {
    xt = value.toString();
    update();
  }

  void clearEditInfo() {
    xt = "-";
    xtEdit = "";
    update();
  }

  void setEdit(data) {
    xtEdit = data.toString();
    update();
  }
}
